import java.util.*;
public class Solution {
    public Rational evalRPN(String[] tokens) {
        //后缀表达式
        //建假栈
        Rational Re []= new Rational[2*tokens.length];
        int flag = 0;
        for(int i=0;i<tokens.length&&!tokens[i].equals("");i++){
            if (isNumeric(tokens[i])){
                int num=Integer.parseInt(tokens[i]);
                Re [flag] = new Rational(num,1);
                flag++;
                Re [flag] = new Rational();
            }
            else {
                //取栈顶元素(出栈)
                flag--;
                Rational a=new Rational(Re[flag].numerator,Re[flag].denominator);
                Re [flag] = new Rational();
                Rational b=new Rational(Re[flag-1].numerator,Re[flag-1].denominator);
                //calculate(a,b,tokens[i]);
                Re [flag-1]=calculate(a,b,tokens[i]);
            }
        }
        //到最后栈里只剩一个元素，所以取栈顶就可以
        return Re [0];
    }
    public Rational calculate(Rational a,Rational b,String opera) {
        Rationalcalculate RC = new Rationalcalculate();
        switch (opera) {

            case "+":
                return RC.add(a,b);
            case "-":
                return RC.sub(b,a);
            case "x":
            case "*":
                return RC.muti(a,b);
            case "/":
                return RC.div(b,a);
            default:
                return new Rational();
        }
    }
    public boolean isNumeric(String checkStr) {
        try {
            Integer.parseInt(checkStr);
            return true; // Did not throw, must be a number
        } catch (NumberFormatException err) {
            return false; // Threw, So is not a number
        }
    }
    }
